﻿/* 
 * Copyright 2012 Pieter Roggemans
 *
 * This file is part of OWNMonitor.
 *
 * OWNMonitor is free software: you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, version 3 of the License.
 *
 * OWNMonitor is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with OWNMonitor. 
 * If not, see http://www.gnu.org/licenses/.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;        // Needed for BindingList
using System.Xml.Serialization;     // Added to use serialisation
using System.IO;                    // Added to use serialisation to file
using OWNLib;

namespace OWNLib
{
    public class OWNProject
    {
        public BindingList<Where> Wheres { get; set; }
        public string Name { get; set; }
        public string IpAddress { get; set; }
        public int IpPort { get; set; }
        public bool MonitorOnly { get; set; }

        public OWNProject()
        {
            IpPort = 20000;
            MonitorOnly = false;
            Wheres = new BindingList<Where>();
        }

        public BindingList<Where> FindWheres(string whoId)
        {
            BindingList<Where> wheres = new BindingList<Where>();

            foreach (Where where in Wheres)
            {
                if (where.WhoId == whoId)
                    wheres.Add(where);
            }
            return wheres;
        }

        public Where FindWhere(string whoId, string whereId)
        {
            foreach (Where where in Wheres)
            {
                if (where.WhoId == whoId && where.WhereId == whereId)
                    return where;
            }
            return new Where(whoId, whereId, "Unknown");
        }

        public void SaveXml(string filePath)
        {
            // First create the directory
            FileInfo filePathInfo = new FileInfo(filePath);
            filePathInfo.Directory.Create();

            // Serialize wheres to file
            XmlSerializer serializer = new XmlSerializer(typeof(OWNProject));
            using (TextWriter tw = new StreamWriter(filePathInfo.FullName))
                serializer.Serialize(tw, this);
        }

        public static OWNProject LoadXml(string filePath)
        {
            // Deserialize wheres
            OWNProject project = null;

            XmlSerializer serializer = new XmlSerializer(typeof(OWNProject));
            using (TextReader tr = new StreamReader(filePath))
            {
                project = (OWNProject)serializer.Deserialize(tr);
            }

            return project;
        }

        public static Where[] LoadFromTxt(string path, string separator)
        {
            // Deserialize wheres
            BindingList<Where> wheres = new BindingList<Where>();

            using (TextReader tr = new StreamReader(path))
            {
                string line, whoId, whereId, name;
                string[] values;
                char[] sepChars = separator.ToCharArray();
                char[] trimChars = "\" ".ToCharArray();

                // Read and display lines from the file until the end of the file is reached.
                int i = 0;
                while ((line = tr.ReadLine()) != null)
                {
                    values = line.Split(sepChars);

                    if (values.Length != 3)
                        throw new Exception("Error importing text file: no 3 columns on row " + i);

                    whoId = values[0].Trim(trimChars);
                    whereId = values[1].Trim(trimChars);
                    name = values[2].Trim(trimChars);

                    // Eerste lijn is lijn met titels
                    if (i == 0)
                    {
                        if(whoId.ToUpper() != "WHOID" || whereId.ToUpper() != "WHEREID" || name.ToUpper() != "NAME")
                            throw new Exception("Error importing text file: first line should contain column names: WhoId, WhereId and Name" + i);
                    }
                    else
                        wheres.Add(new Where(whoId, whereId, name));

                    i++;
                }
            }

            return wheres.ToArray();
        }
    }

    [Serializable()]
    public class Where
    {
        public string WhoId { get; set; }
        public string WhereId { get; set; }
        public string Code { get; set; }

        public Where(string whoId, string whereId, string code)
        {
            WhoId = whoId;
            WhereId = whereId;
            Code = code;
        }

        public Where()
        {
        }

        public override string ToString()
        {
            return Code + " (" + WhereId + ")";
        }
    }

}
